package com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.R;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.adapter.ChatMessageAdapter;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.databinding.ActivityChatWithBinding;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.model.ChatMessage;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.model.UserViewModel;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils.GlobalViewModelStore;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils.MyApplication;

import java.util.ArrayList;
import java.util.List;

//聊天界面
public class ActivityChatWith extends AppCompatActivity {

    private ActivityChatWithBinding binding;
    private RecyclerView chatRecyclerView;
    private EditText messageEditText;
    private Button sendButton;
    private Button backButton;
    private List<ChatMessage> chatMessages;
    private ChatMessageAdapter adapter;
    private long myId = 123456;
    private long otherId = 654321;

    private UserViewModel userViewModel;


    @SuppressLint({"MissingInflatedId", "WrongViewCast"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityChatWithBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(binding.getRoot(), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 获取全局共享的 UserViewModel
        setUserViewModel();

        chatRecyclerView = findViewById(R.id.chatRecyclerView);
        messageEditText = findViewById(R.id.messageEditText);
        sendButton = findViewById(R.id.sendButton);
        backButton = findViewById(R.id.backButton);

        chatMessages = new ArrayList<>();
        adapter = new ChatMessageAdapter(this, chatMessages);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        chatRecyclerView.setLayoutManager(layoutManager);
        chatRecyclerView.setAdapter(adapter);

        // 添加模拟的对方消息
        long currentTime = System.currentTimeMillis();
        chatMessages.add(new ChatMessage("你好", false, currentTime,"AI"));
        adapter.notifyDataSetChanged();

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = messageEditText.getText().toString().trim();
                if (!message.isEmpty()) {
                    long currentTime = System.currentTimeMillis();
                    chatMessages.add(new ChatMessage(message, true, currentTime,"我"));
                    adapter.notifyDataSetChanged();
                    messageEditText.setText("");
                    chatRecyclerView.scrollToPosition(chatMessages.size() - 1);
                }
            }
        });

        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理返回逻辑，这里可以添加返回上一页的代码
                finish();
            }
        });
    }

    private void setUserViewModel(){
        //用户身份
        // 获取全局共享的 UserViewModel
        userViewModel = GlobalViewModelStore
                .getInstance(MyApplication.getInstance())
                .get(UserViewModel.class);
    }
}